package org.ymjava.medical.wx.web;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ymjava.medical.core.util.ResponseUtil;
import org.ymjava.medical.db.AllPackage.biz.FavoriteForUserBiz;
import org.ymjava.medical.db.AllPackage.biz.WrongReportBiz;
import org.ymjava.medical.db.AllPackage.entity.FavoriteForUser;
import org.ymjava.medical.db.AllPackage.entity.WrongReport;

import java.util.Date;

@RestController
@RequestMapping("/wx_answer/favorite")
public class FavoriteController {

    private final Log logger = LogFactory.getLog(FavoriteController.class);

    @Autowired
    FavoriteForUserBiz favoriteForUserBiz;

    @Autowired
    WrongReportBiz wrongReportBiz;

    @GetMapping("/add")
    public Object favorite(Integer userId,Integer questionId){

        logger.info(userId);
        logger.info(questionId);

        FavoriteForUser favoriteForUser = new FavoriteForUser();
//        favoriteForUser.setAddTime(new Date());
        favoriteForUser.setDeleted(0);
        favoriteForUser.setQuestionId(questionId);
        favoriteForUser.setUserId(userId);

        FavoriteForUser favoriteForUser1 = favoriteForUserBiz.selectOne(favoriteForUser);
        if(favoriteForUser1 == null){
            favoriteForUser.setAddTime(new Date());
            favoriteForUserBiz.insert(favoriteForUser);
        }else{
            favoriteForUser.setAddTime(new Date());
            favoriteForUserBiz.updateById(favoriteForUser);
        }

        return ResponseUtil.ok(favoriteForUser);
    }


    @GetMapping("/add_report")
    public Object add_report(Integer userId,Integer questionId){


        logger.info(userId);
        logger.info(questionId);

        WrongReport report = new WrongReport();
        report.setDeleted(0);
        report.setQuestionId(questionId);
        report.setUserId(userId);

        WrongReport report1 = wrongReportBiz.selectOne(report);
        if(report1 == null){
            report.setAddTime(new Date());
            wrongReportBiz.insert(report);
        }else{
            report.setAddTime(new Date());
            wrongReportBiz.updateById(report);
        }

        return ResponseUtil.ok(report);
    }

}
